//
//  BGRegisterViewController.m
//  bigMall
//
//  Created by ios on 16/7/21.
//  Copyright © 2016年 thinklev. All rights reserved.
//

#import "BGRegisterViewController.h"
#import "BGBasicUrl.h"
//#import "UIImageView+WebCache.h"
#import "BGDateResult.h"
#import "BGHttpTool.h"
#import "AFNetworking.h"

@interface BGRegisterViewController () {
    
    UITextField *userNameFiled;//用户名
    UITextField *phoneFiled;//手机号
    UITextField *imageFiled;//图形验证码
    UIImageView *imageView;//图形的获取
    UITextField *messageFiled;//短信
    UITextField *pwdFiled;//密码
    UITextField *querenpwdFiled;//确认了密码
    UIButton *messageBtn;//短信的按钮
    NSTimer *_timer;
    
}

@end

@implementation BGRegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.title = @"注册";
    
    //创建基本的视图
    [self createUI];
    
}

- (void)createUI {
    
    for (int i = 1; i <= 7; i++) {
        
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(21, (i - 1) * 64 + 64, self.view.bounds.size.width - 42, 1)];
        
        if (i == 1) {
            
            view.hidden = YES;
            
        }
        
        view.backgroundColor = [UIColor whiteColor];
        
        view.alpha = 0.3;
        
        [self.view addSubview:view];
        
    }
    //用户名
    UILabel *UserNameLab = [[UILabel alloc] initWithFrame:CGRectMake(21, 64 / 3 + 64 - 64 + 64, BGWidth / 5, 64 / 3)];
    
    UserNameLab.text = @"用户名:";
    UserNameLab.font = [UIFont systemFontOfSize:15];
    UserNameLab.textAlignment = NSTextAlignmentLeft;
    [self.view addSubview:UserNameLab];
    
    userNameFiled = [[UITextField alloc] initWithFrame:CGRectMake(UserNameLab.right, UserNameLab.y, 140, UserNameLab.height)];
    
    userNameFiled.clearButtonMode = UITextFieldViewModeAlways;
    userNameFiled.placeholder = @"请输入用户名";
    [self.view addSubview:userNameFiled];
    
    UITextField *filed1 = [[UITextField alloc] initWithFrame:CGRectMake(userNameFiled.x, userNameFiled.bottom, BGWidth - 42 - UserNameLab.right, 25)];
    filed1.userInteractionEnabled = NO;
    filed1.placeholder = @"*5-16位,允许字母、数字、下划线";
    filed1.font = [UIFont systemFontOfSize:12];
    [self.view addSubview:filed1];
    
    //手机号
    UILabel *pnoneLab = [[UILabel alloc] initWithFrame:CGRectMake(21, 256 / 3 + 65 - 64 + 64, BGWidth / 5, 64 / 3)];
    
    pnoneLab.text = @"手机号:";
    pnoneLab.font = [UIFont systemFontOfSize:15];
    pnoneLab.textAlignment = NSTextAlignmentLeft;
    [self.view addSubview:pnoneLab];
    
    phoneFiled = [[UITextField alloc] initWithFrame:CGRectMake(pnoneLab.right, pnoneLab.y, 140, pnoneLab.height)];
    
    phoneFiled.placeholder = @"请输入手机号";
    
    [self.view addSubview:phoneFiled];
    
    //iamge
    UILabel *iamgeLab = [[UILabel alloc] initWithFrame:CGRectMake(21, 64 * 7 / 3 + 66 - 64 + 64, BGWidth / 5, 64 / 3)];
    
    iamgeLab.text = @"验证码:";
    iamgeLab.font = [UIFont systemFontOfSize:15];
    iamgeLab.textAlignment = NSTextAlignmentLeft;
    [self.view addSubview:iamgeLab];
    
    imageFiled = [[UITextField alloc] initWithFrame:CGRectMake(iamgeLab.right, iamgeLab.y, BGWidth / 2.5, iamgeLab.height)];
    
    imageFiled.placeholder = @"请输验证码";
    
    [self.view addSubview:imageFiled];
    
    imageView = [[UIImageView alloc] initWithFrame:CGRectMake(imageFiled.right, iamgeLab.y - 15, BGWidth - 21 - imageFiled.right, iamgeLab.height + 30)];
    
    imageView.contentMode = UIViewContentModeScaleToFill;
    
    imageView.userInteractionEnabled = YES;
    
//    NSTimeInterval a= [[NSDate date] timeIntervalSince1970] * 1000;
//    
//    NSString *url = [BGBasicUrl getBasicUrl];
    
//    [imageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/bigmall/tools/imgvc/type/forgetLoginPwd?time=%f",url,a]]];
    
    [self.view addSubview:imageView];
    
    //添加手势
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
    
    [tap addTarget:self action:@selector(imageClick)];
    [self imageClick];
    
    [imageView addGestureRecognizer:tap];
    //message
    UILabel *messageLab = [[UILabel alloc] initWithFrame:CGRectMake(21, 10 * 64 / 3 + 65 - 64 + 64, BGWidth / 5, 64 / 3)];
    
    messageLab.text = @"验证码:";
    messageLab.font = [UIFont systemFontOfSize:15];
    messageLab.textAlignment = NSTextAlignmentLeft;
    [self.view addSubview:messageLab];
    
    messageFiled = [[UITextField alloc] initWithFrame:CGRectMake(messageLab.right, messageLab.y, BGWidth / 2.5, messageLab.height)];
    
    messageFiled.placeholder = @"请输入验证码";
    
    messageFiled.secureTextEntry = YES;
    
    [self.view addSubview:messageFiled];
    
    messageBtn = [[UIButton alloc] initWithFrame:CGRectMake(messageFiled.right, messageLab.y - 15, BGWidth - 21 - messageFiled.right, messageLab.height + 30)];
    
    [messageBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
    
    messageBtn.titleLabel.font = [UIFont systemFontOfSize:12];
    
    messageBtn.tintColor = [UIColor whiteColor];
    
    messageBtn.backgroundColor = [UIColor redColor];
    
    messageBtn.clipsToBounds = YES;
    
    messageBtn.layer.cornerRadius = 10;
    
    messageBtn.selected = NO;
    
    [messageBtn addTarget:self action:@selector(clickMessage) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:messageBtn];
    //pwd
    UILabel *pwdLab = [[UILabel alloc] initWithFrame:CGRectMake(21, 13 * 64 / 3 + 65 - 64 + 64, BGWidth / 5, 64 / 3)];
    
    pwdLab.text = @"密码:";
    pwdLab.font = [UIFont systemFontOfSize:15];
    pwdLab.textAlignment = NSTextAlignmentLeft;
    [self.view addSubview:pwdLab];
    
    pwdFiled
    = [[UITextField alloc] initWithFrame:CGRectMake(pwdLab.right, pwdLab.y, 140, pwdLab.height)];
    
    pwdFiled.placeholder = @"请输入密码";
    pwdFiled.secureTextEntry = YES;
    
    [self.view addSubview:pwdFiled];
    UITextField *textFiled1 = [[UITextField alloc] initWithFrame:CGRectMake(pwdFiled.x, pwdFiled.bottom, BGWidth - 42 - UserNameLab.right, 25)];
    textFiled1.userInteractionEnabled = NO;
    textFiled1.placeholder = @"*6-18位,仅且同时包含数字和字母";
    textFiled1.font = [UIFont systemFontOfSize:12];
    [self.view addSubview:textFiled1];

    
    //确认的pwd
    UILabel *querenpwdLab = [[UILabel alloc] initWithFrame:CGRectMake(21, 16 * 64 / 3 + 66 - 64 + 64, BGWidth / 5, 64 / 3)];
    
    querenpwdLab.text = @"确认密码:";
    querenpwdLab.font = [UIFont systemFontOfSize:15];
    querenpwdLab.textAlignment = NSTextAlignmentLeft;
    [self.view addSubview:querenpwdLab];
    
    querenpwdFiled = [[UITextField alloc] initWithFrame:CGRectMake(querenpwdLab.right, querenpwdLab.y, 160, querenpwdLab.height)];
    
    querenpwdFiled.placeholder = @"请确认密码";
    querenpwdFiled.secureTextEntry = YES;
    
    [self.view addSubview:querenpwdFiled];
    
    UITextField *provideFiled = [[UITextField alloc] initWithFrame:CGRectMake(21, 16 * 64 / 3 + 117 - 64 + 64, self.view.bounds.size.width - 42, 12)];
    
    provideFiled.placeholder = @"(密码长度6-16位,数字、字母、字符至少包含两种)";
    
    provideFiled.textAlignment = NSTextAlignmentCenter;
    
    provideFiled.userInteractionEnabled = NO;
    
    provideFiled.font = [UIFont systemFontOfSize:12];
    
    [self.view addSubview:provideFiled];

    
    //注册的Btn
    UIButton *xiugaiBtn = [[UIButton alloc] initWithFrame:CGRectMake(21, provideFiled.bottom + 37, self.view.bounds.size.width - 42, 50)];
    
    xiugaiBtn.clipsToBounds = YES;
    
    xiugaiBtn.layer.cornerRadius = 10;
    
    [xiugaiBtn setTitle:@"注册" forState:UIControlStateNormal];
    
    [xiugaiBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    xiugaiBtn.backgroundColor = [UIColor redColor];
    
    [xiugaiBtn addTarget:self action:@selector(zhuce) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:xiugaiBtn];

    
}
- (void)restoreBtn {
    
    messageBtn.enabled = YES;
    [messageBtn setTitle:@"发送验证码" forState:UIControlStateNormal];
    messageBtn.backgroundColor = [UIColor redColor];
    
    if (messageBtn.enabled == YES) {
        [_timer invalidate];
    }
    
}
- (void)zhuce {
    
    [BGDateResult registerWithUserName:userNameFiled.text phone:phoneFiled.text pwd:pwdFiled.text apwd:querenpwdFiled.text phoneCode:messageFiled.text success:^(NSString *status, NSString *msg, NSString *user_id) {
        
        if ([status  isEqual: @"1"]) {
            
            [self.navigationController popViewControllerAnimated:YES];
            [self showMessage:msg];
            
        }else {
            
            [self showMessage:msg];
            
        }
        
    } failure:^(NSError *error) {
        
        
        
    }];
    
}

- (void)showMessage:(NSString *)message {
    
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:message preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
    
    [alertController addAction:okAction];

    [self presentViewController:alertController animated:YES completion:nil];
    
}

- (void)imageClick {
    
    NSString *url = [BGBasicUrl getBasicUrl];
    
    [BGHttpTool POSTImage:[NSString stringWithFormat:@"%@/index.php?s=/bigmall/tools/imgvc/type/bigmallRegister/id/1.html",url] paramenters:nil progress:^(NSProgress *progress) {
        
    } success:^(id responseObject) {
        
        imageView.image = [UIImage imageWithData:responseObject];
        
    } failure:^(NSError *error) {
        
    }];
    
}
- (void)startTimer {
    
    //timer
    _timer = [NSTimer scheduledTimerWithTimeInterval:10.0 target:self selector:@selector(restoreBtn) userInfo:nil repeats:YES];
    [[NSRunLoop mainRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];
    
}

- (void)clickMessage {
    
            [BGDateResult getPhoneCodeWithPhoneNum:phoneFiled.text andPicCode:imageFiled.text andType:@"bigmallRegister" success:^(NSDictionary *dic) {
                
                if ([dic[@"status"]isEqual:@"1"]) {
                    
                    [messageBtn  setTitle:@"已发送" forState:UIControlStateNormal];
                    
                    messageBtn.backgroundColor = [UIColor lightGrayColor];
                    
                    messageBtn.selected = YES;
                    
                    [self startTimer];
                    
                    [self showMessage:dic[@"msg"]];
                
                }
                
                if (![dic[@"status"] isEqual: @"1"]) {
                    
                    [self showMessage:dic[@"msg"]];
                    [self imageClick];
                    
                }
                
            } failure:^(NSError *error) {
                
                
                
            }];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {

    [userNameFiled resignFirstResponder];
    [phoneFiled resignFirstResponder];
    [imageFiled resignFirstResponder];
    [messageFiled resignFirstResponder];
    [pwdFiled resignFirstResponder];
    [querenpwdFiled resignFirstResponder];
    
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
